package framework.office.apache.poi.doc;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Iterator;

import org.apache.poi.POIXMLDocument;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.model.io.HWPFOutputStream;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Table;
import org.apache.poi.hwpf.usermodel.TableCell;
import org.apache.poi.hwpf.usermodel.TableIterator;
import org.apache.poi.hwpf.usermodel.TableRow;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xwpf.usermodel.IBodyElement;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
public class docTest {
	public static String filestr1 = "1.doc";
	public static String filestr2 = "2.doc";
	public static File file1;
	public static File file2;
	public static FileInputStream fis;
	public static FileOutputStream fos;
	public static void ini() throws Exception {
		filestr1 = "1.doc";
		filestr2 = "2.doc";
		file1 = new File(filestr1);
		file2 = new File(filestr2);
		fis = new FileInputStream(file1);
		fos = new FileOutputStream(file2);
	}
	public static void main(String[] args) throws Exception {
		outdoc4();
	}
	public static void indoc() throws Exception {
		FileInputStream fis = new FileInputStream(new File("1.doc"));
		HWPFDocument di = new HWPFDocument(fis);
		System.out.println(di.getDocumentText());
		System.out.println(di.getText());
		fis.close();
	}
	public static void outdoc1() throws Exception {
		String filetowrite = "2.doc";
		String content = "撒旦解放了第三";
		byte[] b = content.getBytes();
		ByteArrayInputStream bais = new ByteArrayInputStream(b);
		POIFSFileSystem fs = new POIFSFileSystem();
		FileOutputStream out = new FileOutputStream(filetowrite);
		fs.writeFilesystem(out);
		bais.close();
		out.close();
	}
	public static void outdoc2() throws Exception {
		String filetowrite = "2.doc";
		String content = "大家斯洛伐克队就算了";
		FileOutputStream fos = new FileOutputStream(filetowrite);
		OPCPackage pkg = POIXMLDocument.openPackage("1.doc");
		HWPFOutputStream dt = new HWPFOutputStream();
		XWPFDocument doc = new XWPFDocument(pkg);
		fos.write(content.getBytes());
		dt.writeTo(fos);
	}
	public static void outdoc3() throws Exception {
		FileInputStream fis = new FileInputStream(filestr1);
		POIFSFileSystem pfs = new POIFSFileSystem(fis);
		HWPFDocument hwpf = new HWPFDocument(pfs);
		Range range = hwpf.getRange();
		hwpf.getText();
		TableIterator it = new TableIterator(range);
		while (it.hasNext()) {
			Table tb = it.next();
			for (int i = 0; i < tb.numRows(); i++) {
				TableRow tr = tb.getRow(i);
				System.out.println();
				for (int j = 0; j < tr.numCells(); j++) {
					TableCell td = tr.getCell(j);
					for (int k = 0; k < td.numParagraphs(); k++) {
						Paragraph para = td.getParagraph(k);
						String s = para.text();
						System.out.print(s.substring(0, s.length() - 1));
					}
				}
			}
		}
	}
	public static void outdoc4() throws Exception {
		ini();
		POIFSFileSystem poifs = new POIFSFileSystem(fis);
		HWPFDocument doc = new HWPFDocument(poifs);
		Range range = doc.getRange();
		Paragraph p = null;
		int pnum = range.numParagraphs();
		for (int i = 0; i < pnum; i++) {
			p = range.getParagraph(i);
			System.out.println(p.text());
			System.out.println("****");
		}
	}
	public static void indoc1() throws Exception {
		OPCPackage p1 = OPCPackage.open(new FileInputStream(new File(filestr1)));
		// OPCPackage pack = POIXMLDocument.openPackage(filestr1);
		XWPFDocument doc = new XWPFDocument(p1);
		Iterator<IBodyElement> it = doc.getBodyElements().iterator();
		while (it.hasNext()) {
			IBodyElement ib = it.next();
			System.out.println(ib.getBody());
		}
	}
}
